package cn.nfwx.tv.helper;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.RequestExecutor;

import java.util.List;

import cn.nfwx.tv.R;
import cn.nfwx.tv.util.AppUtil;

/**
 * Created by dell on 2018/1/19.
 */

public class PermissionHelper {

    public static final int REQUESTCODE_EXTERNAL_STORAGE = 300;
    public static final int REQUESTCODE_WIFI = 301;

    private static PermissionHelper instance;

    public static PermissionHelper getInstance() {
        if (instance == null) {
            synchronized (PermissionHelper.class) {
                if (instance == null) {
                    instance = new PermissionHelper();
                }
            }
        }
        return instance;
    }

    public void requestStoragePermission(final Fragment fragment, Action<List<String>> grantedAction, Action<List<String>> deniedAction) {
        AndPermission.with(fragment)
                .runtime()
                .permission(
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE
                )
                .rationale(new Rationale<List<String>>() {
                    @Override
                    public void showRationale(Context context, List<String> data, RequestExecutor executor) {
                        showStorageRationaleDialog(fragment, executor);
                    }
                })
                .onGranted(grantedAction)
                .onDenied(deniedAction)
                .start();
    }

    /**
     * wifi的下载权限
     *
     * @param activity
     * @param grantedAction
     * @param deniedAction
     */
    public void requestWifiPerission(Activity activity, Action<List<String>> grantedAction, Action<List<String>> deniedAction) {
        AndPermission.with(activity)
                .runtime()
                .permission(Manifest.permission.ACCESS_WIFI_STATE,
                        Manifest.permission.CHANGE_WIFI_STATE,
                        Manifest.permission.ACCESS_NETWORK_STATE,
                        Manifest.permission.CHANGE_NETWORK_STATE,
                        Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.ACCESS_FINE_LOCATION)
                .rationale(new Rationale<List<String>>() {
                    @Override
                    public void showRationale(Context context, List<String> data, RequestExecutor executor) {
                        AppCompatActivity activity = AppUtil.getAppCompActivity(context);
                        showWifiLocationRationaleDialog(activity, executor);
                    }
                })
                .onGranted(grantedAction)
                .onDenied(deniedAction)
                .start();
    }

    /**
     * 获取定位权限
     *
     * @param activity
     * @param grantedAction
     * @param deniedAction
     */
    public void requestLocationPerission(Activity activity, Action<List<String>> grantedAction, Action<List<String>> deniedAction) {
        AndPermission.with(activity)
                .runtime()
                .permission(Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.ACCESS_FINE_LOCATION)
                .rationale(new Rationale<List<String>>() {
                    @Override
                    public void showRationale(Context context, List<String> data, RequestExecutor executor) {
                        AppCompatActivity activity = AppUtil.getAppCompActivity(context);
                        showLocationRationaleDialog(activity, executor);
                    }
                })
                .onGranted(grantedAction)
                .onDenied(deniedAction)
                .start();
    }

    /**
     * SD弹出设置
     *
     * @param fragment
     */
    public void showStorageSettingDialog(Fragment fragment) {
        String title = fragment.getString(R.string.permission_permission_sd_title);
        String msg = fragment.getString(R.string.permission_permission_sd);
        showCommonSettingDialog(fragment.getActivity(), fragment.getChildFragmentManager(), title, msg, 400);
    }

    /**
     * 网络位置弹出设置
     *
     * @param activity
     */
    public void showWifiLocationSettingDialog(AppCompatActivity activity) {
        if (activity == null) {
            return;
        }
        String title = activity.getString(R.string.permission_wifi_location_title);
        String msg = activity.getString(R.string.permission_wifi_location_setting);
        showCommonSettingDialog(activity, activity.getSupportFragmentManager(), title, msg, 401);
    }

    /**
     * SD重新弹窗
     *
     * @param fragment
     * @param executor
     */
    public void showStorageRationaleDialog(Fragment fragment, RequestExecutor executor) {
        String title = fragment.getString(R.string.permission_sd_title);
        String msg = fragment.getString(R.string.permission_sd);
        showCommonRationaleDialog(fragment.getChildFragmentManager(), executor, title, msg);
    }

    /**
     * 网络位置重新申请弹窗
     *
     * @param activity
     * @param executor
     */
    public void showWifiLocationRationaleDialog(AppCompatActivity activity, RequestExecutor executor) {
        String title = activity.getString(R.string.permission_wifi_location_title);
        String msg = activity.getString(R.string.permission_wifi_location);
        showCommonRationaleDialog(activity.getSupportFragmentManager(), executor, title, msg);
    }

    /**
     * 网络位置重新申请弹窗
     *
     * @param activity
     * @param executor
     */
    public void showLocationRationaleDialog(AppCompatActivity activity, RequestExecutor executor) {
        String title = activity.getString(R.string.permission_location_title);
        String msg = activity.getString(R.string.permission_location);
        showCommonRationaleDialog(activity.getSupportFragmentManager(), executor, title, msg);
    }

    private void showCommonSettingDialog(final Activity activity, FragmentManager fragmentManager, String title, String msg, final int requestCode) {
        DialogFragmentHelper.showConfirmDialog(fragmentManager, title, msg, "设置", "拒绝", false, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                Uri uri = Uri.fromParts("package", activity.getPackageName(), null);
                intent.setData(uri);
                activity.startActivityForResult(intent, requestCode);
            }
        }, null);
    }

    private void showCommonRationaleDialog(FragmentManager fragmentManager, final RequestExecutor executor, String title, String msg) {
        DialogFragmentHelper.showConfirmDialog(fragmentManager, title, msg, "同意", "拒绝", false, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                executor.execute();
            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                executor.cancel();
            }
        });
    }
}
